import Mock from 'mockjs'

import fs from 'fs'
import path from 'path'
// 用户列表
import userList from './userList.js'

const userList1 = JSON.parse(localStorage.getItem('userList')) || userList
import moment from 'moment'
// 获取用户列表接口
Mock.mock('/api/getUserList', 'get', () => {
    const userList1 = JSON.parse(localStorage.getItem('userList')) || userList
    return {
        code: 200,
        data: userList1
    }
})

// 添加用户接口
Mock.mock('/api/addUser', 'post', (body) => {
    let obj = JSON.parse(body.body)
    const userList1 = JSON.parse(localStorage.getItem('userList')) || userList
    if (obj.id) {
        let index = userList1.findIndex(v => {
            return v.id == obj.id
        })

        userList1[index] = obj
    } else {
        obj.number = userList1.length + 1
        obj.id = userList1.length + 1
        obj.userId = userList1.length + 1
        obj.createTime = moment(new Date()).format("yyyy-MM-DD HH:mm:ss")
        obj.updateTime = moment(new Date()).format("yyyy-MM-DD HH:mm:ss")
        obj.onlineDuration = 10
        obj.status = 1
        userList1.unshift(obj)
    }



    localStorage.setItem('userList', JSON.stringify(userList1))
    return {
        code: 200,
        msg: obj.id ? '用户修改成功' : "用户添加成功"
    }
})
// 删除用户
Mock.mock('/api/delUser', 'post', ({ body }) => {
    let obj = JSON.parse(body)
    const userList1 = JSON.parse(localStorage.getItem('userList')) || userList
    let index = userList1.findIndex(v => {
        return v.id == obj
    })
    userList1.splice(index, 1)
    localStorage.setItem('userList', JSON.stringify(userList1))
    return {
        code: 200,
        msg: '删除用户成功'
    }
})
// 角色列表
import roleList from './roleList.js'
const roleList1 = JSON.parse(localStorage.getItem('roleList')) || roleList
Mock.mock('/api/getRoleList', 'get', () => {
    const roleList1 = JSON.parse(localStorage.getItem('roleList')) || roleList
    return {
        code: 200,
        data: roleList1,
        msg: "获取角色列表成功"
    }
})
// 校验角色名称
Mock.mock('/api/roleCheck', 'post', (res) => {
    const roleList1 = JSON.parse(localStorage.getItem('roleList')) || roleList
    let userInfo = JSON.parse(res.body).roleName
    let index = roleList1.findIndex(v => {
        return v.name == userInfo
    })

    if (index == -1) {

        return {
            code: 200,
            msg: '该角色名称未被注册，可以正常添加',
            flag: true

        }
    } else {
        return {
            code: 200,
            msg: '该角色名称已被注册，请重新填写角色名称',
            flag: false
        }
    }
})

// 添加角色
Mock.mock('/api/addRole', 'post', (req) => {
    const roleList1 = JSON.parse(localStorage.getItem('roleList')) || roleList
    let obj = JSON.parse(req.body)
    obj.icon = "/src/assets/imgs/role/cy.png",
        obj.id = roleList1.length + 1
    obj.authorityList = []
    roleList1.unshift(obj)
    localStorage.setItem('roleList', JSON.stringify(roleList1))
    return {
        code: 200,
        msg: '角色添加成功'
    }
})

// 删除角色
Mock.mock('/api/delRole', 'post', ({ body }) => {
    const roleList1 = JSON.parse(localStorage.getItem('roleList')) || roleList
    let id = JSON.parse(body)
    let index = roleList1.findIndex(v => {
        return v.id == id
    })
    roleList1.splice(index, 1)
    localStorage.setItem('roleList', JSON.stringify(roleList1))
    return {
        code: 200,
        msg: '删除角色成功'
    }
})
// 获取所有权限
import authorityList from './authorityList.js'

Mock.mock('/api/getAuthorityList', 'get', () => {
    return {
        code: 200,
        data: authorityList
    }
})
// 分配权限  assignPermissionsAPI

Mock.mock('/api/assignPermissions', 'post', ({ body }) => {
    const roleList1 = JSON.parse(localStorage.getItem('roleList')) || roleList
    let { roleId, authorityList } = JSON.parse(body)
    let index = roleList1.findIndex(r => r.id === roleId)

    if (index != -1) {
        roleList1[index].authorityList = authorityList
        localStorage.setItem('roleList', JSON.stringify(roleList1))
    }

    return {
        "code": 200,
        "msg": "权限分配成功",
    }
})
// 为角色添加用户
Mock.mock('/api/allocationUser', 'post', ({ body }) => {
    let { userIdList, roleId } = JSON.parse(body)
    const userList1 = JSON.parse(localStorage.getItem('userList')) || userList
    userList1.forEach(v => {
        if (v.roleId == roleId) {
            v.roleId = 0
        }
        if (userIdList.includes(v.id)) {
            v.roleId = roleId
        } else {
        }
    })
    localStorage.setItem('userList', JSON.stringify(userList1))
    return {
        "code": 200,
        "msg": "用户分配成功",
    }
})

// 获取席位列表
import seatList from './seatList'
var seatList1 = JSON.parse(localStorage.getItem('seatList')) || seatList
Mock.mock('/api/getSeatList', 'get', () => {
    return {
        code: 200,
        data: seatList1,
        msg: "获取席位列表成功"
    }
})

// 校验席位名称
Mock.mock('/api/seatCheck', 'post', (res) => {

    let userInfo = JSON.parse(res.body).seatName
    let index = seatList1.findIndex(v => {
        return v.name == userInfo
    })

    if (index == -1) {

        return {
            code: 200,
            msg: '该席位名称未被注册，可以正常添加',
            flag: true

        }
    } else {
        return {
            code: 200,
            msg: '该席位名称已被注册，请重新填写角色名称',
            flag: false
        }
    }
})


// 添加席位
Mock.mock('/api/addSeat', 'post', (req) => {
    let obj = JSON.parse(req.body)
    obj.icon = "/src/assets/imgs/role/cy.png",
        obj.id = seatList1.length + 1
    obj.authorityList = []
    seatList1.unshift(obj)
    localStorage.setItem('seatList', JSON.stringify(seatList1))
    return {
        code: 200,
        msg: '席位添加成功'
    }
})

// 删除席位

Mock.mock('/api/delSeat', 'post', ({ body }) => {
    let id = JSON.parse(body)
    let index = seatList1.findIndex(v => {
        return v.id == id
    })
    seatList1.splice(index, 1)
    localStorage.setItem('seatList', JSON.stringify(seatList1))
    return {
        code: 200,
        msg: '删除角色成功'
    }
})





// 分配权限  assignPermissionsAPI

Mock.mock('/api/assignPermissionsBySeat', 'post', ({ body }) => {

    let { seatId, authorityList } = JSON.parse(body)
    let index = seatList1.findIndex(r => r.id === seatId)
    if (index != -1) {
        seatList1[index].authorityList = authorityList
        localStorage.setItem('seatList', JSON.stringify(seatList1))
    }
    return {
        "code": 200,
        "msg": "权限分配成功",
    }
})